<?php
declare(strict_types=1);
/**
 * MineAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using MineAdmin.
 *
 * @Author X.Mo<root@imoi.cn>
 * @Link   https://gitee.com/xmo/MineAdmin
 */

namespace App\Goods\Controller;

use App\Goods\Service\GoodsInfoService;
use App\Goods\Request\GoodsInfoRequest;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\DeleteMapping;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\PostMapping;
use Hyperf\HttpServer\Annotation\PutMapping;
use Mine\Annotation\Auth;
use Mine\Annotation\RemoteState;
use Mine\Annotation\OperationLog;
use Mine\Annotation\Permission;
use Mine\MineController;
use Psr\Http\Message\ResponseInterface;
use Mine\Middlewares\CheckModuleMiddleware;
use Hyperf\HttpServer\Annotation\Middleware;

/**
 * 商品列表控制器
 * Class GoodsInfoController
 */
#[Controller(prefix: "goods/info"), Auth]
#[Middleware(middleware: CheckModuleMiddleware::class)]
class GoodsInfoController extends MineController
{
    /**
     * 业务处理服务
     * GoodsInfoService
     */
    #[Inject]
    protected GoodsInfoService $service;

    
    /**
     * 列表
     * @return ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[GetMapping("index"), Permission("goods:info, goods:info:index")]
    public function index(): ResponseInterface
    {
        return $this->success($this->service->getPageList($this->request->all()));
    }

    /**
     * 回收站列表
     * @return ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[GetMapping("recycle"), Permission("goods:info:recycle")]
    public function recycle(): ResponseInterface
    {
        return $this->success($this->service->getPageListByRecycle($this->request->all()));
    }

    /**
     * 单个或批量真实删除数据 （清空回收站）
     * @return ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[DeleteMapping("realDelete"), Permission("goods:info:realDelete"), OperationLog]
    public function realDelete(): ResponseInterface
    {
        return $this->service->realDelete((array) $this->request->input('ids', [])) ? $this->success() : $this->error();
    }

    /**
     * 单个或批量恢复在回收站的数据
     * @return ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PutMapping("recovery"), Permission("goods:info:recovery"), OperationLog]
    public function recovery(): ResponseInterface
    {
        return $this->service->recovery((array) $this->request->input('ids', [])) ? $this->success() : $this->error();
    }

    /**
     * 新增
     * @param GoodsInfoRequest $request
     * @return ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping("save"), Permission("goods:info:save")]
    public function save(GoodsInfoRequest $request): ResponseInterface
    {
        $form=json_decode('{"form":{"goods_type":10,"spec_type":20,"is_restrict":0,"is_enable_grade":1,"goods_name":"商品名称","imagesIds":[2],"goods_no":"no11122","status":10,"sort":100,"goods_price":1,"stock_num":100,"goods_weight":0,"deduct_stock_type":10,"content":"<p><span style=\"color: rgba(0, 0, 0, 0.85); font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;; font-size: 13px; text-align: right; background-color: rgb(255, 255, 255);\">商品详情</span></p>","serviceIds":[10001,10003],"sales_initial":0,"is_points_gift":1,"is_points_discount":1,"is_ind_delivery_type":0,"is_alone_grade":0,"delivery_type":[10],"delivery_id":10001,"specData":{"specList":[{"key":0,"spec_name":"颜色","valueList":[{"key":0,"groupKey":0,"spec_value":"黑"},{"key":1,"groupKey":0,"spec_value":"红"}]},{"key":1,"spec_name":"内存","valueList":[{"key":0,"groupKey":1,"spec_value":"16G"},{"key":1,"groupKey":1,"spec_value":"32G"}]}],"skuList":[{"image_id":0,"goods_price":16,"line_price":26,"stock_num":266,"goods_weight":26,"goods_sku_no":"2666661","tempId":"黑_16G","skuKeys":[{"groupKey":0,"valueKey":0},{"groupKey":1,"valueKey":0}],"spec_value_0":"黑","spec_value_1":"16G"},{"image_id":0,"goods_price":32,"line_price":32,"stock_num":32331,"goods_weight":32,"goods_sku_no":"322221","tempId":"黑_32G","skuKeys":[{"groupKey":0,"valueKey":0},{"groupKey":1,"valueKey":1}],"spec_value_0":"黑","spec_value_1":"32G"},{"image_id":0,"goods_price":16,"line_price":26,"stock_num":266,"goods_weight":26,"goods_sku_no":"26666","tempId":"红_16G","skuKeys":[{"groupKey":0,"valueKey":1},{"groupKey":1,"valueKey":0}],"spec_value_0":"红","spec_value_1":"16G"},{"image_id":0,"goods_price":32,"line_price":32,"stock_num":32332,"goods_weight":32,"goods_sku_no":"22","tempId":"红_32G","skuKeys":[{"groupKey":0,"valueKey":1},{"groupKey":1,"valueKey":1}],"spec_value_0":"红","spec_value_1":"32G"}]},"categoryIds":[1]}}',true);
        return $this->success($this->service->saveData($form['form']));
    }

    /**
     * 更新
     * @param int $id
     * @param GoodsInfoRequest $request
     * @return ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PutMapping("update/{id}"), Permission("goods:info:update"), OperationLog]
    public function update(int $id, GoodsInfoRequest $request): ResponseInterface
    {
        return $this->service->update($id, $request->all()) ? $this->success() : $this->error();
    }

    /**
     * 读取数据
     * @param int $id
     * @return ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[GetMapping("read/{id}"), Permission("goods:info:read")]
    public function read(int $id): ResponseInterface
    {
        //{
        //    "status": 200,
        //    "message": "success",
        //    "data": {
        //        "goodsInfo": {
        //            "goods_id": 1,
        //            "goods_type": 10,
        //            "goods_name": "商品名称",
        //            "goods_no": "no11122",
        //            "video_id": 0,
        //            "video_cover_id": 0,
        //            "selling_point": "",
        //            "spec_type": 20,
        //            "goods_price_min": "16.00",
        //            "goods_price_max": "32.00",
        //            "line_price_min": "26.00",
        //            "line_price_max": "32.00",
        //            "stock_total": 65195,
        //            "deduct_stock_type": 10,
        //            "is_restrict": 0,
        //            "restrict_total": 0,
        //            "restrict_single": 0,
        //            "content": "<p><span style=\"color: rgba(0, 0, 0, 0.85); font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;; font-size: 13px; text-align: right; background-color: rgb(255, 255, 255);\">商品详情<\/span><\/p>",
        //            "sales_initial": 0,
        //            "sales_actual": 0,
        //            "delivery_id": 10001,
        //            "is_points_gift": 1,
        //            "is_points_discount": 1,
        //            "is_alone_points_discount": 0,
        //            "points_discount_config": "",
        //            "is_enable_grade": 1,
        //            "is_alone_grade": 0,
        //            "alone_grade_equity": [],
        //            "is_ind_delivery_type": 0,
        //            "delivery_type": [
        //                10
        //            ],
        //            "status": 10,
        //            "sort": 100,
        //            "is_delete": 0,
        //            "store_id": 10001,
        //            "create_time": "2024-08-30 20:03:13",
        //            "update_time": "2024-08-30 20:03:13",
        //            "goods_images": [
        //                {
        //                    "file_id": 2,
        //                    "group_id": 0,
        //                    "channel": 10,
        //                    "storage": "local",
        //                    "domain": "",
        //                    "file_type": 10,
        //                    "file_name": "3.jpg",
        //                    "file_path": "10001\/20240830\/35763c1f4c44280f86f4046846f791aa.jpg",
        //                    "file_size": 76145,
        //                    "file_ext": "jpg",
        //                    "cover": "",
        //                    "uploader_id": 0,
        //                    "is_recycle": 0,
        //                    "is_delete": 0,
        //                    "store_id": 10001,
        //                    "create_time": "2024-08-30 19:52:59",
        //                    "update_time": "2024-08-30 19:52:59",
        //                    "preview_url": "http:\/\/ysapi.shop.czsen.com\/uploads\/10001\/20240830\/35763c1f4c44280f86f4046846f791aa.jpg",
        //                    "external_url": "http:\/\/ysapi.shop.czsen.com\/uploads\/10001\/20240830\/35763c1f4c44280f86f4046846f791aa.jpg"
        //                }
        //            ],
        //            "goods_image": "http:\/\/ysapi.shop.czsen.com\/uploads\/10001\/20240830\/35763c1f4c44280f86f4046846f791aa.jpg",
        //            "goods_sales": 0,
        //            "categoryIds": [
        //                1
        //            ],
        //            "specList": [
        //                {
        //                    "spec_id": 1,
        //                    "spec_name": "颜色",
        //                    "key": 0,
        //                    "valueList": [
        //                        {
        //                            "key": 0,
        //                            "groupKey": 0,
        //                            "spec_value_id": 1,
        //                            "spec_value": "黑"
        //                        },
        //                        {
        //                            "key": 1,
        //                            "groupKey": 0,
        //                            "spec_value_id": 2,
        //                            "spec_value": "红"
        //                        }
        //                    ]
        //                },
        //                {
        //                    "spec_id": 2,
        //                    "spec_name": "内存",
        //                    "key": 1,
        //                    "valueList": [
        //                        {
        //                            "key": 0,
        //                            "groupKey": 1,
        //                            "spec_value_id": 3,
        //                            "spec_value": "16G"
        //                        },
        //                        {
        //                            "key": 1,
        //                            "groupKey": 1,
        //                            "spec_value_id": 4,
        //                            "spec_value": "32G"
        //                        }
        //                    ]
        //                }
        //            ],
        //            "serviceIds": [
        //                10001,
        //                10003
        //            ],
        //            "isSpecLocked": false,
        //            "images": [
        //                {
        //                    "id": 1,
        //                    "goods_id": 1,
        //                    "image_id": 2,
        //                    "store_id": 10001,
        //                    "create_time": "2024-08-30 20:03:13",
        //                    "file": {
        //                        "file_id": 2,
        //                        "group_id": 0,
        //                        "channel": 10,
        //                        "storage": "local",
        //                        "domain": "",
        //                        "file_type": 10,
        //                        "file_name": "3.jpg",
        //                        "file_path": "10001\/20240830\/35763c1f4c44280f86f4046846f791aa.jpg",
        //                        "file_size": 76145,
        //                        "file_ext": "jpg",
        //                        "cover": "",
        //                        "uploader_id": 0,
        //                        "is_recycle": 0,
        //                        "is_delete": 0,
        //                        "store_id": 10001,
        //                        "create_time": "2024-08-30 19:52:59",
        //                        "update_time": "2024-08-30 19:52:59",
        //                        "preview_url": "http:\/\/ysapi.shop.czsen.com\/uploads\/10001\/20240830\/35763c1f4c44280f86f4046846f791aa.jpg",
        //                        "external_url": "http:\/\/ysapi.shop.czsen.com\/uploads\/10001\/20240830\/35763c1f4c44280f86f4046846f791aa.jpg"
        //                    }
        //                }
        //            ],
        //            "skuList": [
        //                {
        //                    "id": 1,
        //                    "goods_sku_id": "1_3",
        //                    "goods_id": 1,
        //                    "image_id": 0,
        //                    "goods_sku_no": "2666661",
        //                    "goods_price": "16.00",
        //                    "line_price": "26.00",
        //                    "stock_num": 266,
        //                    "goods_weight": 26,
        //                    "goods_props": [
        //                        {
        //                            "group": {
        //                                "name": "颜色",
        //                                "id": "1"
        //                            },
        //                            "value": {
        //                                "name": "黑",
        //                                "id": "1"
        //                            }
        //                        },
        //                        {
        //                            "group": {
        //                                "name": "内存",
        //                                "id": "2"
        //                            },
        //                            "value": {
        //                                "name": "16G",
        //                                "id": "3"
        //                            }
        //                        }
        //                    ],
        //                    "spec_value_ids": [
        //                        "1",
        //                        "3"
        //                    ],
        //                    "store_id": 10001,
        //                    "create_time": "2024-08-30 20:03:13",
        //                    "update_time": "2024-08-30 20:03:13",
        //                    "image": null
        //                },
        //                {
        //                    "id": 2,
        //                    "goods_sku_id": "1_4",
        //                    "goods_id": 1,
        //                    "image_id": 0,
        //                    "goods_sku_no": "322221",
        //                    "goods_price": "32.00",
        //                    "line_price": "32.00",
        //                    "stock_num": 32331,
        //                    "goods_weight": 32,
        //                    "goods_props": [
        //                        {
        //                            "group": {
        //                                "name": "颜色",
        //                                "id": "1"
        //                            },
        //                            "value": {
        //                                "name": "黑",
        //                                "id": "1"
        //                            }
        //                        },
        //                        {
        //                            "group": {
        //                                "name": "内存",
        //                                "id": "2"
        //                            },
        //                            "value": {
        //                                "name": "32G",
        //                                "id": "4"
        //                            }
        //                        }
        //                    ],
        //                    "spec_value_ids": [
        //                        "1",
        //                        "4"
        //                    ],
        //                    "store_id": 10001,
        //                    "create_time": "2024-08-30 20:03:13",
        //                    "update_time": "2024-08-30 20:03:13",
        //                    "image": null
        //                },
        //                {
        //                    "id": 3,
        //                    "goods_sku_id": "2_3",
        //                    "goods_id": 1,
        //                    "image_id": 0,
        //                    "goods_sku_no": "26666",
        //                    "goods_price": "16.00",
        //                    "line_price": "26.00",
        //                    "stock_num": 266,
        //                    "goods_weight": 26,
        //                    "goods_props": [
        //                        {
        //                            "group": {
        //                                "name": "颜色",
        //                                "id": "1"
        //                            },
        //                            "value": {
        //                                "name": "红",
        //                                "id": "2"
        //                            }
        //                        },
        //                        {
        //                            "group": {
        //                                "name": "内存",
        //                                "id": "2"
        //                            },
        //                            "value": {
        //                                "name": "16G",
        //                                "id": "3"
        //                            }
        //                        }
        //                    ],
        //                    "spec_value_ids": [
        //                        "2",
        //                        "3"
        //                    ],
        //                    "store_id": 10001,
        //                    "create_time": "2024-08-30 20:03:13",
        //                    "update_time": "2024-08-30 20:03:13",
        //                    "image": null
        //                },
        //                {
        //                    "id": 4,
        //                    "goods_sku_id": "2_4",
        //                    "goods_id": 1,
        //                    "image_id": 0,
        //                    "goods_sku_no": "22",
        //                    "goods_price": "32.00",
        //                    "line_price": "32.00",
        //                    "stock_num": 32332,
        //                    "goods_weight": 32,
        //                    "goods_props": [
        //                        {
        //                            "group": {
        //                                "name": "颜色",
        //                                "id": "1"
        //                            },
        //                            "value": {
        //                                "name": "红",
        //                                "id": "2"
        //                            }
        //                        },
        //                        {
        //                            "group": {
        //                                "name": "内存",
        //                                "id": "2"
        //                            },
        //                            "value": {
        //                                "name": "32G",
        //                                "id": "4"
        //                            }
        //                        }
        //                    ],
        //                    "spec_value_ids": [
        //                        "2",
        //                        "4"
        //                    ],
        //                    "store_id": 10001,
        //                    "create_time": "2024-08-30 20:03:13",
        //                    "update_time": "2024-08-30 20:03:13",
        //                    "image": null
        //                }
        //            ],
        //            "video": null,
        //            "videoCover": null
        //        }
        //    }
        //}
        return $this->success($this->service->read($id));
    }

    /**
     * 单个或批量删除数据到回收站
     * @return ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[DeleteMapping("delete"), Permission("goods:info:delete"), OperationLog]
    public function delete(): ResponseInterface
    {
        return $this->service->delete((array) $this->request->input('ids', [])) ? $this->success() : $this->error();
    }

    /**
     * 更改数据状态
     * @return ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PutMapping("changeStatus"), Permission("goods:info:update"), OperationLog]
    public function changeStatus(): ResponseInterface
    {
        return $this->service->changeStatus(
            (int) $this->request->input('goods_id'),
            (string) $this->request->input('statusValue'),
            (string) $this->request->input('statusName', 'status')
        ) ? $this->success() : $this->error();
    }

    /**
     * 数字运算操作
     * @return ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PutMapping("numberOperation"), Permission("goods:info:update"), OperationLog]
    public function numberOperation(): ResponseInterface
    {
        return $this->service->numberOperation(
            (int) $this->request->input('goods_id'),
            (string) $this->request->input('numberName'),
            (int) $this->request->input('numberValue', 1),
        ) ? $this->success() : $this->error();
    }

    /**
     * 数据导入
     * @return ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping("import"), Permission("goods:info:import")]
    public function import(): ResponseInterface
    {
        return $this->service->import(\App\Goods\Dto\GoodsInfoDto::class) ? $this->success() : $this->error();
    }

    /**
     * 下载导入模板
     * @return ResponseInterface
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping("downloadTemplate")]
    public function downloadTemplate(): ResponseInterface
    {
        return (new \Mine\MineCollection)->export(\App\Goods\Dto\GoodsInfoDto::class, '模板下载', []);
    }

    /**
     * 数据导出
     * @return ResponseInterface
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping("export"), Permission("goods:info:export"), OperationLog]
    public function export(): ResponseInterface
    {
        return $this->service->export($this->request->all(), \App\Goods\Dto\GoodsInfoDto::class, '导出数据列表');
    }


    /**
     * 远程万能通用列表接口
     * @return ResponseInterface
     */
    #[PostMapping("remote"), RemoteState(true)]
    public function remote(): ResponseInterface
    {
        return $this->success($this->service->getRemoteList($this->request->all()));
    }
}